package com.henu.zmb.DBhelper;

import android.widget.BaseAdapter;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/5/18.
 */
// 单个添加
public class DBhelper<E extends BmobObject> {
    public void Add(E e){
        e.save(new SaveListener<String>() {
            @Override
            public void done(String objectId,BmobException e) {
                if(e == null){
                    System.out.print("添加数据成功，返回objectId为："+objectId);
                }else{
                    System.out.print("创建数据失败：" + e.getMessage());
                }
            }
        });
    }
    // 添加多个
    public void Add( List<BmobObject> le){
        new BmobBatch().insertBatch(le).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> o, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < o.size(); i++) {
                        BatchResult result = o.get(i);
                        BmobException ex = result.getError();
                        if (ex == null) {
                            System.out.print("第" + i + "个数据批量添加成功：" + result.getCreatedAt() + "," + result.getObjectId() + "," + result.getUpdatedAt());
                        } else {
                            System.out.print("第" + i + "个数据批量添加失败：" + ex.getMessage() + "," + ex.getErrorCode());
                        }
                    }
                } else {
                    System.out.print("当前集合添加失败！");
                }
            }
        });
    }
    // 删除单个
    public void Delete(E e){
        e.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    System.out.print("bmob更新成功");
                }else{
                    System.out.print("bmob更新失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
    // 删除多个
    public void Delete(List<BmobObject> le){
        new BmobBatch().deleteBatch(le).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> o, BmobException e) {
                if(e==null){
                    for(int i=0;i<o.size();i++){
                        BatchResult result = o.get(i);
                        BmobException ex =result.getError();
                        if(ex==null){
                            System.out.print("第"+i+"个数据批量删除成功");
                        }else{
                            System.out.print("第"+i+"个数据批量删除失败："+ex.getMessage()+","+ex.getErrorCode());
                        }
                    }
                }else{
                    System.out.print("bmob删除失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
    // 更新单个
    public void Update(E e , String objectId){
        e.update(objectId, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    System.out.print("bmob更新成功");
                }else{
                    System.out.print("bmob更新失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
    /*
    public void  SearchALL(ArrayList<E> result){

        BmobQuery<E> query = new BmobQuery<E>();
        //查询playerName叫“比目”的数据
        //query.addWhereEqualTo("playerName", "比目");
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<E>() {
            @Override
            public void done(List<E> object, BmobException e) {
                if(e==null){
                   result=(object);
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }*/

}
